package com.example.blindinterfacetest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class GestureWindow extends RelativeLayout {
	private float lastX, lastY; //The last event coordinates
	private float beginX, beginY; //The first event coordinates
	private int oneFingerEvents, twoFingerEvents, moreFingerEvents; //The number of events occurred during one gesture
	private int motionLeft,motionUp, motionRight, motionDown; //How many times the said motions are performed during this gesture

	public GestureWindow(Context context) {
		super(context);
		init();
	}

	public GestureWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public GestureWindow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		//Find focus of possibly multiple fingers
		int pointerCount = ev.getPointerCount();
		float focusX = 0, focusY = 0; 
		MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
		for(int i=0;i<pointerCount;i++){
			ev.getPointerCoords(i, coords);
			focusX += coords.x;
			focusY += coords.y;
		}
		focusX /= pointerCount;
		focusY /= pointerCount;
			
		//Perform action specific stuff
		double motionAngle;
		switch(ev.getAction()){
		case MotionEvent.ACTION_DOWN:
			oneFingerEvents = 0;
			twoFingerEvents = 0;
			moreFingerEvents = 0;

			beginX = focusX;
			beginY = focusY;
			lastX = focusX;
			lastY = focusY;
			
			motionUp = 0;
			motionDown = 0;
			motionLeft = 0;
			motionRight = 0;
			break;
		case MotionEvent.ACTION_MOVE:
			//EKRANIN Y AXISI TERS
			motionAngle = Math.atan2(lastY - focusY, focusX - lastX); //Get the motion angle
			
			//Determine the motion direction
			if(-Math.PI/4 < motionAngle && motionAngle <= Math.PI/4)
				motionRight++;
			else if(Math.PI/4 < motionAngle && motionAngle <= 3*Math.PI/4)
				motionUp++;
			else if(-3*Math.PI/4 < motionAngle && motionAngle <= -Math.PI/4)
				motionDown++;
			else
				motionLeft++;
			
			lastX = focusX;
			lastY = focusY;
			
			break;
		case MotionEvent.ACTION_UP:
			if(FloatMath.sqrt((focusY-beginY)*(focusY-beginY)+(focusX-beginX)*(focusX-beginX)) > 300){ //TODO: MAKE THIS DISTANCE GENERIC
				int globalPointerCount = 0; //The overall pointer count for this gesture
				if(oneFingerEvents > twoFingerEvents + moreFingerEvents)
					globalPointerCount = 1;
				else if(twoFingerEvents > oneFingerEvents + moreFingerEvents)
					globalPointerCount = 2;
				
				int threshold = 4*(motionUp + motionDown + motionLeft + motionRight)/5; //TODO: DETERMINE THIS PERCENTAGE
				//PUT ACTIONS TO PERFORM UPON GESTURES HERE
				if(motionUp > threshold)
					System.out.println("swipe up with "+globalPointerCount);
				else if(motionDown > threshold){
					if(globalPointerCount == 2){
						((Activity)getContext()).startActivity(new Intent(getContext(),AudialHome.class));
						((Activity)getContext()).finish();
					}
				}
				else if(motionLeft > threshold)
					System.out.println("swipe left with "+globalPointerCount);
				else if(motionRight > threshold)
					System.out.println("swipe right with "+globalPointerCount);
			}
			break;
		}

		//Record the number of one/two/more finger events
		switch(ev.getPointerCount()){
		case 1:
			oneFingerEvents++;
			break;
		case 2:
			twoFingerEvents++;
			break;
		default:
			moreFingerEvents++;
			break;
		}

		return true;
	}
}
